import { observable, action ,toJS,runInAction} from "mobx";





// 编辑商铺数据
export default class ShopsEdit{
  
  // 商铺id
  @observable id=0;
  @observable loading=false;
  // 商品数据
  @observable data={};
  @observable dataDouble=false;//服务器数据对比是否修改过
  
  // 是否展示大图
  @observable previewImageVisible=false;
  // 展示的大图地址
  @observable previewImageUrl=false;
  
  // 显示快递公司输入框
  @observable companyVisible=false;
  // 邮费相关
  @observable postage={};

  @observable activeKey=0;

  constructor(root){
    this.root = root;
  }
  
  @action set=(k,v)=>{
    const arr = k.split("-");
    const loop = (obj,arr,v)=>{
      const temp = arr.splice(0,1);
      if(arr.length){
        obj = obj[temp];
        loop(obj,arr,v);
      }else{
        obj[temp] = v;
      }
    };
    loop(this,arr,v);
  };
  
  // 初始化数据
  @action init=()=>this._init();
  _init(){
    this.set("data",{
      shops:{
       
      }
    });
    this.set("companyVisible",false);
    this.root.set("dataDouble",false);
    this.root.set("loading",false);
    this.root.set("activeKey",0);
    // 如果有id则获取商品信息
    if(this.id){
      this._info();
    }
  }
  // 商品信息
  _info(){
    this.root.ajax.post({
      url:this.root.url,
      data:{act:"admin-shops-select",id:this.id},
      success:json=>{
        if(json.status==="ok"){        
          this.set("data",json.data);
          this.set("dataDouble",json.data);
        }
      }
    });
  }

  // insert-update
  @action submit=()=>{
    const {shops}=this.data;
    if(!shops.tit){
      this.root.antd.modal.error({title:"错误",content:"缺少名称"});
      return;
    }else if(!shops.address){
      this.root.antd.modal.error({title:"错误",content:"缺少地区"});
      return;
    }else if(!shops.id){
      this.root.antd.modal.error({title:"错误",content:"缺少用户ID"});
      return;
    }else if(!shops.image){
      this.root.antd.modal.error({title:"错误",content:"缺少商铺图片"});
      return;
    } 

    this.root.set("loading",true);
    let data={
      act:"admin-shops-update",
      data:{
        shops:{}
      }
    };
    if(this.id){//update
      data.id=this.id;
      data.data.shops= toJS(this._submitDouble());
      data.data.shops.id=shops.id;
      if(JSON.stringify(this.data.postage) !== JSON.stringify(this.dataDouble.postage)){
        data.data.postage=toJS(this.data.postage);
      }
    }else{//insert
      data.act="admin-shops-insert";
      data.data.shops= toJS(shops);
      data.data.shops.id=shops.id;
    }
    // console.log("data",data);
    // return;
    data.data = JSON.stringify(toJS(data.data));
    this.root.ajax.post({
      url:this.root.url,
      data:data,
      success:json=>{
        this.root.set("loading",false);
        this.root.alert(json);
        if(json.status !== "ok"){return;}
        this.set("dataDouble",JSON.parse(data.data));
      }
    });
  }
  
 
  // 上传基本信息解析
  _submitDouble(){
    const shopsKeys = Object.keys(this.data.shops);
    const data={};
    shopsKeys.map(key=>{
      if(!this.dataDouble || this.data.shops[key] !== this.dataDouble.shops[key]){
        data[key]=this.data.shops[key];
      }
    });
    return data;
  }

  // 上传图片
  @action uploadChange=(info,tp)=>{
    if(info.file.status === "uploading"){
      this.root.set("loading",true);
    }else if (info.file.status === "done"){
      this.root.set("loading",false);
      this.set("data-shops-image",info.file.response.data);
    }else if (info.file.status === "error") {
      this.root.alert({status:"error","msg":`${info.file.name} 图片上传失败!.`});
      this.root.set("loading",false);
    }else{
      this.root.set("loading",false);
    }
  }

  @action getBase64(file) {
    return new Promise((resolve, reject) => {
      const reader = new FileReader();
      reader.readAsDataURL(file);
      reader.onload = () => resolve(reader.result);
      reader.onerror = error => reject(error);
    });
  }

  

}